﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicUniversityBiz;
namespace LogicUniversity.clerks
{
    public partial class RetrivalList_Detail : System.Web.UI.Page
    {

        RetrievalListController retController;
        List<RetrievalDetailData> result;

        protected void Page_PreInit(Object sender, EventArgs e)
        {
            //(Session["Role"]
            if (Session["Role"] as string != "Clerk")
            {
                Response.Redirect("~/Login.aspx");
            }

        }

        public CompositeReterivalData PassData
        {
            get { return (CompositeReterivalData)(this.ViewState["passData"]); }
            set { this.ViewState["passData"] = value; }
        }
        //public int Batch_No
        //{
        //    get { return Convert.ToInt16(this.ViewState["batch_No"]); }
        //    set { this.ViewState["batch_No"] = value; }
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                result = new List<RetrievalDetailData>();
                retController = new RetrievalListController();
                PassData = PreviousPage.PassData;
                lblItemDesc.Text = PassData.Item_Description;
                result = retController.getRetrievalDetail(PassData.Item_Code,PassData.Batch_No);
                grdRetrivalDetail.DataSource = result;
                grdRetrivalDetail.DataBind();
                this.ViewState["result"] = result;
                
                //originalactual();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            retController = new RetrievalListController();
            int modifiedActual = 0 ;

            try
            {
                result = (List<RetrievalDetailData>)(this.ViewState["result"]);
                updateResult();
                foreach (RetrievalDetailData d in result)
                {

                    modifiedActual += d.Actual;
                }
                int adjustedAmount = PassData.RetrievedQty - modifiedActual;
                EFFascade fascade = new EFFascade();
                Catelogue catelogue = fascade.getCatelogueOf(PassData.Item_Code);
                if (catelogue.Balance - Math.Abs(adjustedAmount) < 0)
                {
                    throw new Exception();
                }
                else
                {
                    if (retController.updateRetrievalDetail(result, adjustedAmount))
                    {
                        Response.Redirect("~/clerks/RetrievalList.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
                return;
            }

        }

        //original actual
        //protected void originalActual()
        //{
        //    result = (List<RetrievalDetailData>)(this.ViewState["result"]);
        //    int originalActual = 0;
        //    foreach (RetrievalDetailData d in result)
        //    {
        //        originalActual += d.Actual;
        //    }
        //    this.ViewState["originalActual"] = originalActual;
        //}

        //updating list
        protected void updateResult()
        {
            //List<RequisitionCustom> reqList = new List<RequisitionCustom>();
            
            result = (List<RetrievalDetailData>)(this.ViewState["result"]);
            int i = 0;
            foreach (GridViewRow r in grdRetrivalDetail.Rows)
            {
                RequisitionCustom c = new RequisitionCustom();
                //reqList[i].Item_Description = r.Cells[0].Text;
                //reqList[i].Needed =Convert.ToInt16(r.Cells[1].Text);
                //c.Balance = Convert.ToInt16(r.Cells[2].Text);
                ((Label)r.FindControl("lblError")).Text = null;
                try
                {
                    int actual=Convert.ToInt16(((TextBox)r.FindControl("txtActual")).Text);
                    int needed = result[i].Needed;
                    int actualBeforeChange = result[i].Actual;
                    EFFascade fascade = new EFFascade();
                    Catelogue catelogue = fascade.getCatelogueOf(PassData.Item_Code);
                    int actualAfter=actual-actualBeforeChange;
                    if (actualAfter < 0)
                    {
                        if ((catelogue.Balance + actualAfter) < 0)
                        {
                            throw new Exception();
                        }
                    }
                    
                    if (actual > needed)
                    {
                        ((Label)r.FindControl("lblError")).Text = "Actual Quantity cannot be greater than needed.";
                        throw new Exception();
                    }
                    
                    result[i].Actual = actual;
                    
                    
                }
                catch (Exception ef)
                {
                    ((Label)r.FindControl("lblError")).Text = "Illegal Quantity";
                    throw new Exception("Illegal Actual Quantity");
                }
                //c.Item_Code = r.Cells[4].Text;
                //reqList.Add(c);
                i++;
            }
            this.ViewState["result"] = result;
        }

        protected void lnkBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("RetrievalList.aspx");
        }
    }
}